<?php
function getChoirText($choir)
{
	$choirtext="";
//	echo $choir." - ";
	if (!(strpos($choir,'B') === false)) $choirtext .= "Boys, ";
	if (!(strpos($choir,'G') === false)) $choirtext .= "Girls, ";
	if (!(strpos($choir,'C') === false)) $choirtext .= "Boys (optional Girls), ";
	if (!(strpos($choir,'L') === false)) $choirtext .= "Lay Clerks, ";
//	echo $choirtext."<br>";
	return substr($choirtext,0,-2);
}

function sendMusicList($email,$html)
{
	$dtNow = new DateTime('now');
	$message = new Message();
	$message->addRecipient($email);
	
	//$message->addRecipient("michael@michaelstoddart.org");
	//$message->addRecipient("michael.stoddart@stnicholascathedral.co.uk");
	$message->setSubject("Provisional Music List - ".$dtNow->format("F j, Y"));
	$message->setFrom("St Nicholas Music Admin<no-reply@stnicholascathedralmusic.co.uk>");
	$message->addStyle("p, td {font-family: sans-serif;vertical-align:top;font-size: 70%;color:#003366;}");
	$message->addStyle("h1{font-family: sans-serif;font-size: 100%;color:#003366;}");
	$message->addStyle("h2{font-family: sans-serif;font-size: 80%;color:#003366;}");
	
	$message->setMessageBody($html);
//	echo $html;
	$message->send();
//	echo "Sent to ".$email."\n";
	
}

error_reporting(E_ALL);
date_default_timezone_set("Europe/London");
ini_set('display_errors', '1');

$FILE_SYSTEM_ROOT = dirname(dirname(__FILE__));

// get config info
$xml = simplexml_load_file($FILE_SYSTEM_ROOT."/config/config.xml");
if(!isset($xml))echo "$xml not set";

$PathsXML = $xml->Paths;
$DOCUMENT_ROOT = $PathsXML->DocumentRoot;


require_once(dirname(__FILE__)."/../../MSWebGUI/Mail/Message.php");




require_once($FILE_SYSTEM_ROOT."/model/classes/database/Database.php");
if(!isset($xml->Database))echo "$xml->Database not set";
$databaseXML = $xml->Database;
$DATABASE = new Database($databaseXML->Server,$databaseXML->Database,$databaseXML->Username,$databaseXML->Password);

require_once($FILE_SYSTEM_ROOT."/model/classes/events/Event.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/utilities/Users.php");
$users = new Users();
$USERS = $users->getUsers();
require_once($FILE_SYSTEM_ROOT."/model/classes/utilities/Locations.php");
$locations = new Locations();
$LOCATIONS = $locations->getLocations();
require_once($FILE_SYSTEM_ROOT."/model/classes/utilities/Involvees.php");
$involvees = new Involvees();
$INVOLVEES = $involvees->getInvolvees();
require_once($FILE_SYSTEM_ROOT."/model/classes/utilities/EventTypes.php");
$eventTypes = new EventTypes();
$EVENTTYPES = $eventTypes->getEventTypes();

require_once($FILE_SYSTEM_ROOT."/model/classes/people/Groups.php");
$groups = new Groups();
$GROUPS = $groups->getGroups();


require_once($FILE_SYSTEM_ROOT."/model/classes/people/Address.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/people/PeopleResultSet.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/people/Person.php");

require_once($FILE_SYSTEM_ROOT."/model/classes/music/MusicResultSet.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/music/MusicEvent.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/music/MusicHistoryRequest.php");

require_once($FILE_SYSTEM_ROOT."/model/classes/events/EventsResultSet.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/events/Event.php");

$date = new DateTime('now');
$dateStr = $date->format("l jS F Y");
$html = "<h1>Forthcoming Music (provisional)</h1>";
$html .= "<p>You will see below a list of the music to be sung at forthcoming services/concerts. ";
$html .= "This is not necessarily final, nor complete, but is to give you an idea of what is coming up. ";
$html .= "For details of the precise timings etc, please read the Choir Schedule email which gives those details. ";
$html .= "I hope you find this useful.";
$html .= "<h2>Services and Concerts from ".$dateStr."</h2>";




$musicresultset = new  MusicResultSet("");
$date = new DateTime('yesterday');
$musicresultset->setStartDate($date);
$music = $musicresultset->getMusic();
//print_r($music);

foreach ($music as $musicevent)
{
//	$DTeventdate = new DateTime($musicevent->getDateTime());
	$Seventdate = $musicevent->getDateTime()->format('l jS F Y  H:i');
	$html .= "<p><b>".$Seventdate."</b><br>\n";
	if ($musicevent->getTitle() != "")      $html .= $musicevent->getTitle()." \n"; 
	if ($musicevent->getChoir() != "")      $html .= "sung by ".getChoirText($musicevent->getChoir())."<br>\n"; 
	else									$html .= "(No choir required)<br>\n";
	if ($musicevent->getPsalms() != "")     $html .= "Psalm ".$musicevent->getPsalms()."<br>\n"; 
	if ($musicevent->getHymns() != "")      $html .= "Hymns ".$musicevent->getHymns()."<br>\n"; 
	if ($musicevent->getIntroit() != "")    $html .= $musicevent->getIntroit()."<br>\n"; 
	if ($musicevent->getResponses() != "")  $html .= $musicevent->getResponses()." Responses<br>\n"; 
	if ($musicevent->getMass() != "")       $html .= $musicevent->getMass()."<br>\n"; 
	if ($musicevent->getCanticles() != "")  $html .= $musicevent->getCanticles()."<br>\n"; 
	if ($musicevent->getAnthem() != "")     $html .= $musicevent->getAnthem()."<br>\n"; 


	$html .= "<br></p>\n";
	

}
//echo $html;

$distributees =  array(1,2,3,4,5,10);  // Boys, Girls, Lay Clerks, Choral Scholars, Deputies and Music Staff
$distribution = new PeopleResultSet($distributees);

$people = $distribution->getPeople();

//sendMusicList("michael.stoddart@stnicholascathedral.co.uk", $html);

$log = "Email sent to:\n";
/*sendMusicList ("michael@michaelstoddart.org",$html);*/
foreach($people as $person)
{

	$emails = $person->getEmails();
	foreach ($emails as $email)
	{
		$log .= $email."\n";
		sendMusicList ($email,$html);
	}

}
$log .= "\n\nMessage sent:\n".$html;

$dtNow = new DateTime('now');
$message = new Message();
$message->addRecipient("michael.stoddart@stnicholascathedral.co.uk");

//$message->addRecipient("michael@michaelstoddart.org");
//$message->addRecipient("michael.stoddart@stnicholascathedral.co.uk");
$message->setSubject("Confirmation - Mail Provisional Music List - ".$dtNow->format("F j, Y"));
$message->setFrom("St Nicholas Music Admin<no-reply@stnicholascathedralmusic.co.uk>");
/*$message->addStyle("p, td {font-family: sans-serif;vertical-align:top;font-size: 70%;color:#003366;}");
$message->addStyle("h1{font-family: sans-serif;font-size: 100%;color:#003366;}");
$message->addStyle("h2{font-family: sans-serif;font-size: 80%;color:#003366;}");*/

$message->setMessageBody($log);
//	echo $html;
$message->send();



?> 